package de.hwrBerlin.dataMining.view;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import de.hwrBerlin.dataMining.constants.ConfigConstants;
import de.hwrBerlin.dataMining.constants.UIConstants;
import de.hwrBerlin.dataMining.dataStructure.DecisionTreeNode;
import de.hwrBerlin.dataMining.view.widgets.combo.ExtendedComboBox;
import de.hwrBerlin.dataMining.view.widgets.table.ExtendedTable;

public class TreeDetails extends Composite {

	private ExtendedTable _table;
	private ExtendedComboBox _devideAtribute;
	private Label _dataContValue;

	private List<Label> _labelsForAttributeValues = new ArrayList<Label>();
	private DecisionTreeNode _selectedNode;

	public TreeDetails(Composite pParent, int pStyle, final boolean pEditable) {
		super(pParent, pStyle);
		this.setLayout(new GridLayout(2, false));

		GridData spanGridData = new GridData(SWT.FILL, SWT.FILL, true, true);
		spanGridData.horizontalSpan = 2;

		GridData spanGridData2 = new GridData(SWT.FILL, SWT.FILL, true, false);
		spanGridData2.horizontalSpan = 2;

		Label nextDevideByAttributeName = new Label(this, SWT.NONE);
		nextDevideByAttributeName.setLayoutData(spanGridData2);
		nextDevideByAttributeName.setText(UIConstants.DEVIDE_BY_ATTRIBUTE);

		_devideAtribute = new ExtendedComboBox(this, SWT.READ_ONLY, true);
		_devideAtribute.setLayoutData(spanGridData2);
		_devideAtribute.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (pEditable && _selectedNode != null) {
					if (_devideAtribute.getSelection() == null) {
						_selectedNode.setOwnAttribute(_selectedNode.getTargetAttribute());
					} else {
						_selectedNode.setOwnAttribute(((String) _devideAtribute.getSelection()).split(" \\(")[0]);
					}
				}
			}
		});

		if (!pEditable) {
			_devideAtribute.getCombo().setEnabled(false);
		}

		new Label(this, SWT.NONE).setLayoutData(spanGridData2);

		_table = new ExtendedTable(this, SWT.BORDER | SWT.FULL_SELECTION, new ListTableShower());
		_table.setLayoutData(spanGridData);
		_table.getTable().setHeaderVisible(true);
		_table.getTable().setLinesVisible(true);

		Label dataCont = new Label(this, SWT.NONE);
		dataCont.setText(UIConstants.COUNT_OF_DATA);

		_dataContValue = new Label(this, SWT.NONE);
		_dataContValue.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, false, false));
		_dataContValue.setText("0");

		new Label(this, SWT.NONE).setLayoutData(spanGridData2);

		Label targetAttributeValues = new Label(this, SWT.NONE);
		targetAttributeValues.setLayoutData(spanGridData2);
		targetAttributeValues.setText(UIConstants.TARGET_ATTRIBUTE_VALUES);
	}

	public void setInput(DecisionTreeNode node) {
		_selectedNode = node;
		if (node != null) {
			_dataContValue.setText(node.getCountOfElements() + "");
			_table.setHeader(node.getSourceTable().getAttributNames());
			int selectedColumn = -1;
			for (int i = 0; node.getSourceTable().getAttributNames().length > i; i++) {
				if (node.getSourceTable().getAttributNames()[i].equals(node.getTargetAttribute())){
					selectedColumn = i;
					break;
				}
			}
			_table.setInput(node.getSourceTable().getRows(), selectedColumn);
			for (Label label : _labelsForAttributeValues) {
				label.dispose();
			}
			_labelsForAttributeValues.clear();
			Map<String, Integer> values = node.getSourceTable().getValueCountsForAttributName(node.getTargetAttribute());
			for (String key : values.keySet()) {
				Label seperator = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL);
				GridData spanGridData = new GridData(SWT.FILL, SWT.FILL, true, false);
				spanGridData.horizontalSpan = 2;
				seperator.setLayoutData(spanGridData);
				_labelsForAttributeValues.add(seperator);

				Label attributeValueName = new Label(this, SWT.NONE);
				attributeValueName.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false));
				attributeValueName.setText("      - " + key);
				_labelsForAttributeValues.add(attributeValueName);

				Label attributeValueCount = new Label(this, SWT.NONE);
				attributeValueCount.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, false, false));
				attributeValueCount.setText(values.get(key) + " / " + node.getCountOfElements());
				_labelsForAttributeValues.add(attributeValueCount);
			}
			_devideAtribute.setInput(new Object[0]);
			List<String> strings = new ArrayList<String>();
			for (String attribute : node.getSourceTable().getAttributNames()) {
				if (!attribute.equals(node.getTargetAttribute())) {
					DecisionTreeNode testNode = new DecisionTreeNode(node.getSourceTable(), attribute, node.getTargetAttribute(),
							ConfigConstants.ELEMENT_COUNT_TO_TERMINATE);
					strings.add(attribute + " (" + testNode.calculateWeightedEntropy() + ")");
				}
			}
			_devideAtribute.setInput(strings.toArray(new Object[0]));
			if (node.getOwnAttribute() != node.getTargetAttribute()) {
				_devideAtribute.setSetlection(node.getOwnAttribute() + " (" + node.calculateWeightedEntropy() + ")");
			}
		}
		else {
			_dataContValue.setText("0");
			_table.setHeader(new String [0]);
			_table.setInput(new String[0]);
			for (Label label : _labelsForAttributeValues) {
				label.dispose();
			}
			_labelsForAttributeValues.clear();
			_devideAtribute.setInput(new Object[0]);
		}
		this.layout();
	}
}
